package com.cuckoo.mall.thus.menuaccount.recently;

import android.support.annotation.Nullable;
import android.text.Html;
import android.widget.ImageView;

import com.bgn.baseframe.utils.BaseStringUtil;
import com.bgn.baseframe.utils.DateUtil;
import com.bgn.baseframe.utils.imageloader.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.Goods;
import com.cuckoo.mall.javabean.Product;
import com.orhanobut.logger.Logger;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by wanglun on 2018/3/3.
 */

public class RecentlyViewAdapter extends BaseQuickAdapter<Product, BaseViewHolder> {


    public RecentlyViewAdapter(@Nullable List<Product> data) {
        super(R.layout.list_item_recently_view, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Product item) {

        /*处理标题*/
       // Logger.d("时间" + helper.getAdapterPosition() + ":" + DateUtil.getFormatString(item.getLast_viewtime(), "yyyy-MM-dd"));
        if (helper.getAdapterPosition() == 0) {
            helper.setGone(R.id.tv_title, true);
            helper.setText(R.id.tv_title, DateUtil.getFormatString(item.getLast_viewtime(), "yyyy-MM-dd"));
        } else {
            Product lastItem = getData().get(helper.getAdapterPosition() - 1);
            if (isSameDay(lastItem.getLast_viewtime(), item.getLast_viewtime())) {
                helper.setGone(R.id.tv_title, false);
                helper.setText(R.id.tv_title, DateUtil.getFormatString(item.getLast_viewtime(), "yyyy-MM-dd"));
            } else {
                helper.setGone(R.id.tv_title, true);
                helper.setText(R.id.tv_title, DateUtil.getFormatString(item.getLast_viewtime(), "yyyy-MM-dd"));
            }
        }

        /*处理分割线*/
        if (helper.getAdapterPosition() == getData().size() - 1) {
            helper.setGone(R.id.view_line, false);
        } else {
            Product next = getData().get(helper.getAdapterPosition() + 1);
            if (isSameDay(next.getLast_viewtime(), item.getLast_viewtime())) {
                helper.setGone(R.id.view_line, true);
            } else {
                helper.setGone(R.id.view_line, false);
            }
        }

        helper.setText(R.id.tv_name, item.getName());
        ImageLoader.load(item.getImage()).into((ImageView) helper.getView(R.id.iv_image));

        helper.setText(R.id.tv_price, item.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(item.getPrice()));

        if (item.getMarket_price() == 0) {
            helper.setText(R.id.tv_max_price, "");
        } else {
            helper.setText(R.id.tv_max_price, BaseStringUtil.addDeleteLineToText(item.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(item.getMarket_price())));
            //helper.setText(R.id.tv_max_price, Html.fromHtml("<strike>" + item.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(item.getMarket_price()) + "</strike>"));
        }

    }

    public boolean isSameDay(long l1, long l2) {
        Date d1 = new Date(l1 * 1000L);
        Date d2 = new Date(l2 * 1000L);
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(d1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(d2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

}
